package com.lemon.mybatisplus.base;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lemon.core.web.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * @author 李猛
 * @datetime 2023/11/12 20:56
 * @description 类对象
 */
public class BaseController<P, D, S extends BaseService<P, D>> {
    @Autowired(required = false)
    private S service;

    /**
     * 添加单条数据
     *
     * @param p
     * @return
     */
    @PostMapping("add")
    @SentinelResource(value = "add")
    public Result<String> add(@RequestBody P p) {
        return service.add(p);
    }

    /**
     * 添加多条数据
     *
     * @param pList
     * @return
     */
    @PostMapping("adds")
    @SentinelResource(value = "adds")
    public Result<String> adds(@RequestBody List<P> pList) {
        return service.adds(pList);
    }

    /**
     * 根据ID删除数据
     *
     * @param id
     * @return
     */
    @DeleteMapping("delById/{id}")
    @SentinelResource(value = "delById")
    public Result<String> delById(@PathVariable("id") Long id) {
        return service.delById(id);
    }

    /**
     * 根据ID集合批量删除数据
     *
     * @param ids
     * @return
     */
    @DeleteMapping("delByIds")
    @SentinelResource(value = "delByIds")
    public Result<String> delByIds(@RequestParam Collection<Long> ids) {
        return service.delByIds(ids);
    }

    /**
     * 根据ID修改数据（空字段不更新）
     *
     * @param p
     * @return
     */
    @PutMapping("update")
    @SentinelResource(value = "update")
    public Result<String> update(@RequestBody P p) {
        return service.update(p);
    }

    /**
     * 根据ID集合批量修改数据（空字段不更新）
     *
     * @param pList
     * @return
     */
    @PutMapping("updates")
    @SentinelResource(value = "updates")
    public Result<String> updates(@RequestBody List<P> pList) {
        return service.updates(pList);
    }

    /**
     * 新增或修改
     *
     * @param p
     * @return
     */
    @RequestMapping(value = "addOrUpdate", method = {RequestMethod.POST, RequestMethod.PUT})
    @SentinelResource(value = "addOrUpdate")
    public Result<String> addOrUpdate(@RequestBody P p) {
        return service.addOrUpdate(p);
    }

    /**
     * 根据ID查询数据
     *
     * @param id
     * @return
     */
    @GetMapping("getById/{id}")
    @SentinelResource(value = "getById")
    public Result<P> getById(@PathVariable("id") Long id) {
        return service.getById(id);
    }

    /**
     * 根据ID集合查询列表数据
     *
     * @param ids
     * @return
     */
    @GetMapping("getByIds")
    @SentinelResource(value = "getByIds")
    public Result<List<P>> getByIds(@RequestParam Collection<Long> ids) {
        return service.getByIds(ids);
    }

    /**
     * 根据Map集合查询数据
     *
     * @param map
     * @return
     */
    @GetMapping("listByMap")
    @SentinelResource(value = "listByMap")
    public Result<List<P>> listByMap(@RequestParam Map<String, Object> map) {
        return Result.ok(service.listByMap(map));
    }

    /**
     * 根据Map集合查询分页数据
     *
     * @param map
     * @return
     */
    @GetMapping("pageByMap")
    @SentinelResource(value = "pageByMap")
    public Result<Page<P>> pageByMap(@RequestParam Map<String, Object> map) {
        return service.pageByMap(map);
    }

    /**
     * 根据实体类查询列表数据
     *
     * @param d
     * @return
     */
    @GetMapping("list")
    @SentinelResource(value = "list")
    public Result<List<P>> list(D d) {
        return service.list(d);
    }

    /**
     * 根据实体类查询分页数据
     *
     * @param d
     * @return
     */
    @GetMapping("page")
    @SentinelResource(value = "page")
    public Result<Page<P>> page(D d) {
        return service.page(d);
    }

    /**
     * 根据实体类查询数据条数
     *
     * @param d
     * @return
     */
    @GetMapping("count")
    @SentinelResource(value = "count")
    public Result<Long> count(D d) {
        return service.count(d);
    }

    /**
     * 启用
     *
     * @param ids
     * @return
     */
    @PutMapping("enable")
    @SentinelResource(value = "enable")
    public Result<String> enable(@RequestBody Collection<Long> ids) {
        return service.enable(ids);
    }

    /**
     * 停用
     *
     * @param ids
     * @return
     */
    @PutMapping("unable")
    @SentinelResource(value = "unable")
    public Result<String> unable(@RequestBody Collection<Long> ids) {
        return service.unable(ids);
    }
}
